package br.com.puertorico.mecanismos.ui.tags;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;

import javax.el.ELContext;
import javax.el.MethodExpression;
import javax.el.ValueExpression;

/**
 * Extra&iacute;do de
 * http://andrewfacelets.blogspot.com/2006/06/creating-composite-controls
 * -with-jsf.html
 *
 * Esta classe &eacute; usada para permitir que express&otilde;es usadas em tags customizadas
 * possam ser resolvidas para uma MethodExpression ao inv&eacute;s de uma
 * ValueExpression.
 *
 * @author <a href="mailto:daniel.camargo@ufms.br">Daniel Camargo</a>
 * @version 1.0
 */
public class MethodValueExpression extends ValueExpression implements
		Externalizable {

	private ValueExpression orig;

	private MethodExpression methodExpression;

	/**
	 * Construtor default da classe.
	 */
	public MethodValueExpression() {
	}

	/**
	 * @param orig
	 *            Expressão original.
	 * @param methodExpression
	 *            Expressão de método.
	 */
	MethodValueExpression(ValueExpression orig,
			MethodExpression methodExpression) {
		this.orig = orig;
		this.methodExpression = methodExpression;
	}

	/**
	 * @see javax.el.ValueExpression#getExpectedType()
	 */
	@Override
	public Class<?> getExpectedType() {
		return orig.getExpectedType();
	}

	/**
	 * @see javax.el.ValueExpression#getType(javax.el.ELContext)
	 */
	@Override
	public Class<?> getType(ELContext ctx) {
		return MethodExpression.class;
	}

	/**
	 * @see javax.el.ValueExpression#getValue(javax.el.ELContext)
	 */
	@Override
	public Object getValue(ELContext ctx) {
		return methodExpression;
	}

	/**
	 * @see javax.el.ValueExpression#isReadOnly(javax.el.ELContext)
	 */
	@Override
	public boolean isReadOnly(ELContext ctx) {
		return orig.isReadOnly(ctx);
	}

	/**
	 * @see javax.el.ValueExpression#setValue(javax.el.ELContext,
	 *      java.lang.Object)
	 */
	@Override
	public void setValue(ELContext ctx, Object val) {
	}

	/**
	 * @see javax.el.Expression#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object val) {
		return orig.equals(val);
	}

	/**
	 * @see javax.el.Expression#getExpressionString()
	 */
	@Override
	public String getExpressionString() {
		return orig.getExpressionString();
	}

	/**
	 * @see javax.el.Expression#hashCode()
	 */
	@Override
	public int hashCode() {
		return orig.hashCode();
	}

	/**
	 * @see javax.el.Expression#isLiteralText()
	 */
	@Override
	public boolean isLiteralText() {
		return orig.isLiteralText();
	}

	/**
	 * @see java.io.Externalizable#readExternal(java.io.ObjectInput)
	 */
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		orig = (ValueExpression) in.readObject();
		methodExpression = (MethodExpression) in.readObject();
	}

	/**
	 * @see java.io.Externalizable#writeExternal(java.io.ObjectOutput)
	 */
	public void writeExternal(ObjectOutput out) throws IOException {
		out.writeObject(orig);
		out.writeObject(methodExpression);
	}
}